import json
from django.core import serializers
from django.shortcuts import render
from django.http import JsonResponse
from django.http import HttpResponse
from django.views import View
from .models import (Article, Tag, Point, Concept, ArticleToTag)


# Create your views here.
class ArticleView(View):
    def get(self, request):
        response = {}
        data_list = []
        q = request.GET.get('q', '')
        try:
            if q == '' or q is None:
                queryset = Article.objects.all().only('title', 'create_date', "author", "summary")
            else:
                queryset = Article.objects.all().filter(title__contains=q).only('title', 'create_date', "author")

            for article in queryset:
                data_list.append({
                    "pk": article.id,
                    "fields": {
                        "title": article.title,
                        "author": article.author,
                        "summary": article.summary
                    }
                })
            # response['data'] = json.loads(serializers.serialize('json', queryset))
            response['data'] = data_list
            response['statu'] = 'success'
            response['code'] = '20000'

        except Exception as e:
            response['code'] = '30000'
            response['statu'] = 'fail'
            response['error_info'] = str(e)

        return JsonResponse(data=response)

    def post(self, request):
        pass


class TagView(View):
    def get(self, request):
        response = {}
        data_list = []
        q = request.GET.get('q', '')
        try:
            if q == '' or q is None:
                queryset = Tag.objects.all().only('name')
            else:
                queryset = Tag.objects.all().filter(name__contains=q).only('name')
            for tag in queryset:
                data_list.append({
                    "pk": tag.pk,
                    "fields": {
                        "name": tag.name,
                    }
                })

            response['data'] = data_list
            response['statu'] = 'success'
            response['code'] = '20000'

        except Exception as e:
            response['code'] = '30000'
            response['statu'] = 'fail'
            response['error_info'] = str(e)

        return JsonResponse(data=response)

    def post(self, request):
        pass


# 术语概念
class ConceptView(View):
    def get(self, request):
        response = {}
        data_list = []
        q = request.GET.get('q', '')
        try:
            if q == '' or q is None:
                queryset = Concept.objects.all().only('name')
            else:
                queryset = Concept.objects.all().filter(name__contains=q).only('name')

            for concept in queryset:
                data_list.append({
                    "pk": concept.pk,
                    "fields": {
                        "name": concept.name
                    }
                })
            response['data'] = data_list
            response['statu'] = 'success'
            response['code'] = '20000'

        except Exception as e:
            response['code'] = '30000'
            response['statu'] = 'fail'
            response['error_info'] = str(e)

        return JsonResponse(data=response)

    def post(self, request):
        pass


# 知识点
class PointView(View):
    def get(self, request):
        response = {}
        q = request.GET.get('q', '')
        data_list = []
        try:
            if q == '' or q is None:
                queryset = Point.objects.all().only('context')
            else:
                queryset = Point.objects.all().filter(context__contains=q).only('name')

            for point in queryset:
                data_list.append({
                    "pk": point.pk,
                    "fields": {
                        "context": point.context
                    }
                })
            response['data'] = data_list
            response['statu'] = 'success'
            response['code'] = '20000'

        except Exception as e:
            response['code'] = '30000'
            response['statu'] = 'fail'
            response['error_info'] = str(e)

        return JsonResponse(data=response)

    def post(self, request):
        pass


# 文章-标签对应
class ArticleToTagView(View):
    def get(self, request):
        response = {}
        q = request.GET.get('q', '')

        data_list = []
        try:
            if q == '' or q is None:
                queryset = ArticleToTag.objects.all()
            else:
                queryset = ArticleToTag.objects.all()

            for article_to_tag in queryset:
                data_list.append({
                    "pk": article_to_tag.pk,
                    "fields": {
                        "article_pk": article_to_tag.article.pk,
                        "tag_pk": article_to_tag.tag.pk,
                        "create_date": article_to_tag.create_date
                    }
                })

            response['data'] = data_list
            # response['data'] = json.loads(serializers.serialize('json', queryset))
            response['statu'] = 'success'
            response['code'] = '20000'

        except Exception as e:
            response['code'] = '30000'
            response['statu'] = 'fail'
            response['error_info'] = str(e)

        return JsonResponse(data=response)

    def post(self, request):
        pass

# # 知识点-术语概念对应
# class PointToConceptView(View):
#
#     def get(self, request):
#         response = {}
#         try:
#             queryset = PointToConcept.objects.all()
#             response['data'] = json.loads(serializers.serialize('json', queryset))
#             response['statu'] = 'success'
#             response['code'] = '20000'
#
#         except Exception as e:
#             response['code'] = '30000'
#             response['statu'] = 'fail'
#             response['error_info'] = str(e)
#
#
#         return JsonResponse(data=response)
#
#     def post(self, request):
#         pass
